package gw.com.android.ui.news;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.gwtsz.gts2.hx.R;

import gw.com.android.ui.BaseActivity;
import gw.com.android.ui.WebFragment;
import gw.com.android.utils.PackStatis;
import www.com.library.app.PushMsgTabFragment;

/**
 * @author jett
 * @since 2017-11-23.
 */
public class TradeNewsActivity extends BaseActivity {

    public static final String PAGE_INDEX = "page_index";
    public static final int CALENDAR = 0;
    public static final int FLASH = 1;
    public static final int TOPNEWS = 2;

    private TradeNewsPagerAdapter mAdapter;
    private ViewPager mViewPager;

    @Override
    protected int getLayoutView() {
        return R.layout.activity_trade_news;
    }

    @SuppressWarnings("ConstantConditions")
    @Override
    protected void initLayoutView() {
        TabLayout mTabs = mTitleBar.tabs;
        mTabs.setVisibility(View.VISIBLE);
        mViewPager = (ViewPager) findViewById(R.id.vp_news);
        mViewPager.setOffscreenPageLimit(2);
        mAdapter = new TradeNewsPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);
        mTabs.setupWithViewPager(mViewPager);

        int page = getIntent().getIntExtra(PAGE_INDEX, 0);
        mViewPager.setCurrentItem(page);

        findViewById(R.id.title_left_icon).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mTabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                mViewPager.setCurrentItem(position);
                if (position == CALENDAR) {
                    PackStatis.getToService("clickFtime", "Fnews", null, null);
                } else if (position == FLASH) {
                    PackStatis.getToService("clickaflash", "Fnews", null, null);
                } else if (position == TOPNEWS) {
                    PackStatis.getToService("clickFanews", "Fnews", null, null);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

    @Override
    protected void initViewData() {
    }

    @Override
    public void onBackPressed() {
        if (mViewPager != null && mAdapter != null) {
            int currentItem = mViewPager.getCurrentItem();
            Fragment item = mAdapter.getItem(currentItem);
            if (item instanceof WebFragment) {
                if (((WebFragment) item).onBackPressed()) {
                    return;
                }
            }
        }
        super.onBackPressed();
    }

    private class TradeNewsPagerAdapter extends FragmentPagerAdapter {

        private PushMsgTabFragment mCalendarFragment;
        private PushMsgTabFragment mFlashFragment;
        private PushMsgTabFragment mTopNewsFragment;

        TradeNewsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case CALENDAR:
                    if (mCalendarFragment == null) {
                        mCalendarFragment = new CalendarFragment();
                    }
                    return mCalendarFragment;
                case FLASH:
                    if (mFlashFragment == null) {
                        mFlashFragment = new FlashFragment();
                    }
                    return mFlashFragment;
                case TOPNEWS:
                    if (mTopNewsFragment == null) {
                        mTopNewsFragment = new TopNewsFragment();
                    }
                    return mTopNewsFragment;
            }
            return new FlashFragment();
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case CALENDAR:
                    return getString(R.string.home_trade_news_calendar);
                case FLASH:
                    return getString(R.string.home_trade_news_flash);
                case TOPNEWS:
                    return getString(R.string.home_trade_news_topnews);
            }
            return super.getPageTitle(position);
        }
    }

}
